package com.fr.chicken.service.Impl;


import com.fr.chicken.dao.*;
import com.fr.chicken.facade.base.BaseResponseDTO;
import com.fr.chicken.facade.info.ChickenListInfo;
import com.fr.chicken.facade.request.CollectRequsetDTO;
import com.fr.chicken.facade.request.UpdateHouseLevelRequsetDTO;
import com.fr.chicken.facade.response.GetChickenFarmInfoResponseDTO;
import com.fr.chicken.manager.configure.ConfigManager;
import com.fr.chicken.model.*;
import com.fr.chicken.service.ChickenFarmService;
import com.fr.chicken.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service("chickenFarmService")
public class ChickenFarmServiceImpl implements ChickenFarmService {

    @Autowired
    private HouseInfoDao houseInfoDao;
    @Autowired
    private ChickenInfoDao chickenInfoDao;
    @Autowired
    private PlayerInfoDao playerInfoDao;
    @Autowired
    private MessageLogDao messageLogDao;
    @Autowired
    private ChickenConfigureDao chickenConfigureDao;
    @Autowired
    private GoodsLogDao goodsLogDao;
    @Autowired
    private ActivationLogDao activationLogDao;
    @Autowired
    private ConfigManager configManager;
    @Override
    public GetChickenFarmInfoResponseDTO getChickenFarmInfo(Long playerId) {
        GetChickenFarmInfoResponseDTO responseDTO = new GetChickenFarmInfoResponseDTO();
        ChickenConfigure chickenConfigure = chickenConfigureDao.selectByPrimaryKey(3);
        HouseInfo houseInfo = houseInfoDao.selectByPlayerId(playerId);
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(playerId);
        if (houseInfo==null){
            responseDTO.faild();
            responseDTO.setMsg("玩家还没有鸡场");
            return responseDTO;
        }
        List<ChickenInfo> chickenInfos1 = chickenInfoDao.selectByHouseId(houseInfo.getId());
        List<ChickenListInfo> chickenListInfos = new ArrayList<>();
        for (ChickenInfo x:chickenInfos1){
            ChickenListInfo chickenListInfo = new ChickenListInfo();
            chickenListInfo.setId(x.getId());
//            int num = (nowDate.intValue()-x.getLastLayEggsTime().intValue())/(3600*chickenConfigure.getValue());
//            int nums = (nowDate.intValue()-x.getLastLayEggsTime().intValue())%(3600*chickenConfigure.getValue());
//            if(num>0){
//                chickenListInfo.setLastTime(x.getLastLayEggsTime() + 3600 * chickenConfigure.getValue()+3600 * chickenConfigure.getValue()*num);
//            }else {
//                chickenListInfo.setLastTime(x.getLastLayEggsTime() + 3600 * chickenConfigure.getValue()+nums);
//            }
            chickenListInfo.setLastTime(x.getLastLayEggsTime()+3600*chickenConfigure.getValue());
            chickenListInfo.setType(x.getType().intValue());
            chickenListInfos.add(chickenListInfo);
        }
        responseDTO.setNickName(playerInfo.getNickName());
        responseDTO.setTime(System.currentTimeMillis() / 1000);
        responseDTO.setHouseInfo(houseInfo);
        responseDTO.setChickenListInfos(chickenListInfos);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO collectFeaces(CollectRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        HouseInfo houseInfo = houseInfoDao.selectByPrimaryKey(requsetDTO.getHouseId());
        if (houseInfo==null){
            responseDTO.setMsg("鸡场不存在");
            responseDTO.faild();
            return responseDTO;
        }
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(houseInfo.getPlayerId());
        if (houseInfo.getPlayerId().equals(requsetDTO.getPlayerId())){

            int num = houseInfoDao.updateCollectFaeces(requsetDTO.getHouseId());
            if (num < 1) {
                throw new RuntimeException("收集失败");
            }
            PlayerInfo playerInfo1 = new PlayerInfo();
            playerInfo1.setFeaces(playerInfo.getFeaces() + houseInfo.getFaeces());
            playerInfo1.setId(houseInfo.getPlayerId());
            int num1 = playerInfoDao.updateByPrimaryKeySelective(playerInfo1);
            if (num1 < 1) {
                throw new RuntimeException("收集失败");
            }
            responseDTO.setMsg("收集成功");
        }else{
            PlayerInfo playerInfo2 = playerInfoDao.selectByPrimaryKey(requsetDTO.getPlayerId());
            if (houseInfo.getFaeces()<=configManager.getIntConfig(20)){
                responseDTO.faild();
                responseDTO.setMsg("手下留情,给别人留点吧");
                return responseDTO;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());
            String nowTime = sdf.format(now.getTime());
            List<MessageLog> messageLogs = messageLogDao.selectByType((byte)5);
            for (MessageLog x:messageLogs){
                if (x.getValue2()==1&&x.getRecipientId()==playerInfo.getId()&&x.getValue1()==requsetDTO.getPlayerId().intValue()&&nowTime.equals(DateUtil.timeStampDateString(x.getSendTime().toString(),"yyyy-MM-dd"))){
                    responseDTO.faild();
                    responseDTO.setMsg("你今天已经帮好友打扫过了");
                    return responseDTO;
                }
            }
            int num = houseInfoDao.updateCollectFaecesByNum(requsetDTO.getHouseId(),1);
            if (num<1){
                throw new RuntimeException("打扫失败");
            }
            playerInfo2.setFeaces(playerInfo2.getFeaces()+1);
            int num1 = playerInfoDao.updateByPrimaryKeySelective(playerInfo2);
            if (num1<1){
                throw new RuntimeException("打扫失败");
            }
            MessageLog messageLog = new MessageLog();
            messageLog.setType((byte)5);
            messageLog.setSenderId((long)0);
            messageLog.setValue1(requsetDTO.getPlayerId().intValue());
            messageLog.setValue2(1);
            messageLog.setValue3(1);
            messageLog.setRecipientId(houseInfo.getPlayerId());
            messageLog.setContent(playerInfo2.getNickName()+"给你打扫过鸡场,收获了1个粪便");
            messageLog.setSendTime(System.currentTimeMillis() / 1000);
            int num2 = messageLogDao.insertSelective(messageLog);
            if (num2<1){
                throw new RuntimeException("发送消息失败");
            }
            responseDTO.setMsg("打扫成功");
        }
        responseDTO.success();
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO collectFeather(CollectRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        HouseInfo houseInfo = houseInfoDao.selectByPrimaryKey(requsetDTO.getHouseId());
        if (houseInfo==null){
            responseDTO.setMsg("鸡场不存在");
            responseDTO.faild();
            return responseDTO;
        }
        ActivationLog activationLog = activationLogDao.selectByInviteeId(houseInfo.getPlayerId());
        if (activationLog==null){
            responseDTO.faild();
            responseDTO.setMsg("不能打扫鸡毛");
            return responseDTO;
        }
        if (activationLog.getInviterId().equals(requsetDTO.getPlayerId())){
            int num = houseInfoDao.updateCollectFeather(requsetDTO.getHouseId());
            if (num<1){
                throw new RuntimeException("收集失败");
            }
            if (houseInfo.getFeatherMax()>0) {
                houseInfo.setFeatherMax(houseInfo.getFeatherMax() - 1);
                int num3 = houseInfoDao.updateByPrimaryKeySelective(houseInfo);
                if (num3 < 1) {
                    throw new RuntimeException("收集失败");
                }
            }
            PlayerInfo playerInfo2 = playerInfoDao.selectByPrimaryKey(requsetDTO.getPlayerId());
            PlayerInfo playerInfo1 = new PlayerInfo();
            playerInfo1.setFeather(playerInfo2.getFeather()+houseInfo.getFeather());
            playerInfo1.setId(requsetDTO.getPlayerId());
            int num1 = playerInfoDao.updateByPrimaryKeySelective(playerInfo1);
            if (num1<1){
                throw new RuntimeException("打扫失败");
            }
            MessageLog messageLog = new MessageLog();
            messageLog.setType((byte)5);
            messageLog.setSenderId((long)0);
            messageLog.setValue1(requsetDTO.getPlayerId().intValue());
            messageLog.setValue2(2);
            messageLog.setValue3(houseInfo.getFeather());
            messageLog.setRecipientId(houseInfo.getPlayerId());
            messageLog.setContent(playerInfo2.getNickName()+"给你打扫过鸡场,收获了"+houseInfo.getFeather()+"个鸡毛");
            messageLog.setSendTime(System.currentTimeMillis() / 1000);
            int num2 = messageLogDao.insertSelective(messageLog);
            if (num2<1){
                throw new RuntimeException("发送消息失败");
            }
            responseDTO.setMsg("打扫成功");
        }else {
            responseDTO.faild();
            responseDTO.setMsg("不能打扫鸡毛");
            return responseDTO;
        }
        responseDTO.success();
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO collectEgg(Long playerId) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        HouseInfo houseInfo = houseInfoDao.selectByPlayerId(playerId);
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(houseInfo.getPlayerId());
        int num = houseInfoDao.updateCollectEgg(houseInfo.getId());
        if (num<1){
            throw new RuntimeException("收集失败");
        }
        PlayerInfo playerInfo1 = new PlayerInfo();
        playerInfo1.setEgg(playerInfo.getEgg()+houseInfo.getEgg());
        playerInfo1.setId(houseInfo.getPlayerId());
        int num1 = playerInfoDao.updateByPrimaryKeySelective(playerInfo1);
        if (num1<1){
            throw new RuntimeException("收集失败");
        }
        responseDTO.success();
        responseDTO.setMsg("收集成功");
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO updateHouseLevel(UpdateHouseLevelRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        GoodsLog goodsLog = goodsLogDao.selectByInfoAndGoodsId(requsetDTO.getCode(),1);
        if (goodsLog==null){
            responseDTO.faild();
            responseDTO.setMsg("鸡场升级码不存在");
            return responseDTO;
        }
        List<ActivationLog> activationLogs = activationLogDao.selectByInviterId(requsetDTO.getPlayerId());
        if (activationLogs.size()< configManager.getIntConfig(12)){
            responseDTO.faild();
            responseDTO.setMsg("推荐用户数量不足");
            return responseDTO;
        }
        HouseInfo houseInfo = houseInfoDao.selectByPlayerId(requsetDTO.getPlayerId());
        if (houseInfo==null){
            responseDTO.faild();
            responseDTO.setMsg("鸡场不存在");
            return responseDTO;
        }
        if (houseInfo.getLevel()==1){
            responseDTO.faild();
            responseDTO.setMsg("鸡场已经升级过了");
            return responseDTO;
        }
        List<ChickenInfo> chickenInfos = chickenInfoDao.selectByHouseId(houseInfo.getId());
        if (chickenInfos.size()<configManager.getIntConfig(3)){
            responseDTO.faild();
            responseDTO.setMsg("鸡数量不足");
            return responseDTO;
        }
        houseInfo.setLevel(1);
        int num = houseInfoDao.updateByPrimaryKeySelective(houseInfo);
        if (num<1){
            throw new RuntimeException("升级失败");
        }
        int num1 = goodsLogDao.deleteByPrimaryKey(goodsLog.getId());
        if (num1<1){
            throw new RuntimeException("升级失败");
        }
        responseDTO.success();
        responseDTO.setMsg("鸡场升级成功");
        return responseDTO;
    }
}
